package org.jeecg.modules.innerApi.zhiYunSdApi.convert;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.util.DateUtils;
import org.jeecg.common.util.qrcode.QRCodeUtil;
import org.jeecg.modules.common.util.Base64Util;
import org.jeecg.modules.innerApi.common.requestDto.CommonParam;
import org.jeecg.modules.innerApi.common.requestDto.DpdlParam;
import org.jeecg.modules.innerApi.common.responseDto.*;
import org.jeecg.modules.innerApi.zhiYunApi.util.ZhiyunApiResponse;
import org.jeecg.modules.innerApi.zhiYunSdApi.responseVo.*;
import org.jeecg.modules.innerApi.zhiYunSdApi.util.ShudianResponse;
import org.jeecg.modules.innerApi.zhiYunSdApi.util.ZhiyunSdApiResponse;
import org.springframework.beans.BeanUtils;

import java.text.ParseException;
import java.util.Base64;
import java.util.Date;

@Slf4j
public class ZySdResponseRtnConvert {

    /**
     * 发票开具返回转换
     * @param response
     * @return
     */
    public static InvoiceRtn sdfpRtnConvert(ZhiyunSdApiResponse response){
        InvoiceRtn rtn=new InvoiceRtn();
        if(response.getCode()== 200){
            JSONObject jsonData=response.getData();
            InvoiceRtn.InvoiceRtnData rtnData=jsonData.toJavaObject(InvoiceRtn.InvoiceRtnData.class);
            //判断返回结果中是否是扫脸认证
            if(StringUtils.isEmpty(rtnData.getFphm())){
                FaceImgRtn faceImgRtn = faceImgRtnConvert(response);
                FaceImgRtn.FaceImgRtnData faceImgRtnData = faceImgRtn.getData();
                if(!StringUtils.isEmpty(faceImgRtnData.getEwm())){
                    rtn.setCode(CommonConstant.DPPT_DL_NEED_FACE.toString());
                    rtn.setMsg("发票开具失败，电票平台需要扫脸认证！");
                } else {
                    rtn.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500.toString());
                    rtn.setMsg("发票开具失败!返回结果异常：" + jsonData.toJSONString());
                    log.error("发票开具失败!返回结果异常：" + jsonData.toJSONString());
                }
                rtn.setSuccess(false);
                return rtn;
            } else {
                //开票成功
                try {
                    String kqrq = jsonData.getString("Kprq");
                    Date kprq = DateUtils.parseDate(kqrq, "yyyy-MM-dd HH:mm:ss");
                    rtnData.setKprq(kprq);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                rtn.setCode(CommonConstant.SC_OK_200.toString());
                rtn.setSuccess(true);
                rtn.setMsg(response.getMsg());
                rtn.setData(rtnData);
            }
        }else{
            rtn.setCode(CommonConstant.SC_INTERNAL_SERVER_ERROR_500.toString());
            rtn.setSuccess(false);
            rtn.setMsg(response.getMsg());
        }
        return rtn;
    }

    /**
     * 登录电票返回转换
     * @param param
     * @param responseStr
     * @return
     */
    public static DpdlRtn loginRtnConvert(DpdlParam param,String responseStr){
        DpdlRtn rtn=new DpdlRtn();
        if(StringUtils.isNotEmpty(responseStr)) {
            JSONObject rtnJson= JSON.parseObject(responseStr);
            Integer code=rtnJson.getIntValue("code");
            String msg=rtnJson.getString("msg");
            if (code==200||code==10009) {
                rtn.setCode("200");
                rtn.setMsg(msg);
                rtn.setSuccess(true);
                String dllx=param.getEwmlx();
                DpdlRtn.DpdlRtnData dpdlRtnData=new DpdlRtn.DpdlRtnData();
                if(StringUtils.isNotEmpty(dllx)){
                    if(dllx.equals("1")||dllx.equals("10")){
                        Object data=rtnJson.get("data");
                        if(data instanceof JSONObject){
                            JSONObject dataJson=rtnJson.getJSONObject("data");
                            dpdlRtnData=dataJson.toJavaObject(DpdlRtn.DpdlRtnData.class);
                            if(dataJson.containsKey("qrcode")){
                                dpdlRtnData.setEwm(dataJson.getString("qrcode"));
                            }
                        }else if(data instanceof String){
                            dpdlRtnData.setEwmid((String)data);
                        }
                        rtn.setData(dpdlRtnData);
                    }
                }
            } else {
                if(code==401){
                    rtn.setCode("401");
                }else {
                    rtn.setCode("500");
                }
                rtn.setMsg(msg);
                rtn.setSuccess(false);
            }
        }
        return rtn;
    }

    /**
     * 获取是否需要人脸识别返回转换
     * @param response
     * @return
     */
    public static SdHqsfxyrlsbcc120Rtn faceStateRtnConvert(String response){
        SdHqsfxyrlsbcc120Rtn rtn=new SdHqsfxyrlsbcc120Rtn();
        if(StringUtils.isNotEmpty(response)){
            rtn=JSONObject.parseObject(response,SdHqsfxyrlsbcc120Rtn.class);
            String code=rtn.getCode();
            if(code.equals("200")){
                rtn.setSuccess(true);
                //获取base64加密的数据
                String dataBase64=rtn.getData();
                String decodeData=Base64Util.decode(dataBase64);
                rtn.setData(decodeData);
                if(decodeData!=null){
                    SdHqsfxyrlsbcc120Rtn.SfslData sfslData=JSONObject.parseObject(decodeData,SdHqsfxyrlsbcc120Rtn.SfslData.class);
                    //获取是否需要扫码
                    rtn.setSfsl(sfslData.getSfsl());
                }
            }else{
                rtn.setSuccess(false);
            }
        }else{
            rtn.setSuccess(false);
            rtn.setMsg("获取电票平台人脸识别信息超时，请稍后重试");
        }
        return rtn;
    }

    /**
     * 获取人脸二维码返回转换
     * @param response
     * @return
     */
    public static FaceImgRtn faceImgRtnConvert(ZhiyunSdApiResponse response){
        FaceImgRtn rtn=new FaceImgRtn();
        if(response.getCode()== 200) {
            rtn.setSuccess(true);
            rtn.setMsg(response.getMsg());
            JSONObject jsonData=response.getData();
            FaceImgRtn.FaceImgRtnData rtnData = jsonData.toJavaObject(FaceImgRtn.FaceImgRtnData.class);
            if("swj".equals(rtnData.getEwmly())){
                try {
                    byte[] ewmBytes = QRCodeUtil.encode(rtnData.getEwm(), 200, 200);
                    String ewmBase64 = Base64.getEncoder().encodeToString(ewmBytes);
                    rtnData.setEwm(ewmBase64);
                } catch (Exception e) {
                    throw new JeecgBootException("生成二维码错误");
                }
            }

            rtn.setData(rtnData);
        }else{
            rtn.setSuccess(false);
            rtn.setMsg(response.getMsg());
        }
        return rtn;
    }

    /**
     * 获企业风险查询返回转换
     * @param response
     * @return
     */
    public static SdQyfxcxcc116Rtn sdQyfxcxcc116RtnConvert(String response){
        SdQyfxcxcc116Rtn rtn=new SdQyfxcxcc116Rtn();
        if(StringUtils.isNotEmpty(response)){
            ZhiyunApiResponse zhiyunApiResponse=JSONObject.parseObject(response,ZhiyunApiResponse.class);
            Integer code=zhiyunApiResponse.getCode();
            if(code==200){
                rtn=zhiyunApiResponse.getData().toJavaObject(SdQyfxcxcc116Rtn.class);
                rtn.setSuccess(true);
                rtn.setMsg(zhiyunApiResponse.getMsg());
            }else{
                rtn.setSuccess(false);
                rtn.setMsg(zhiyunApiResponse.getMsg());
            }
        }else{
            rtn.setSuccess(false);
            rtn.setMsg("获取电票平台人脸识别信息超时，请稍后重试");
        }
        return rtn;
    }

    /**
     * 获取授信额度返回转换
     * @param response
     * @return
     */
    public static CreditRtn creditLineRtnConvert(ZhiyunSdApiResponse response,CommonParam commonParam){
        CreditRtn rtn=new CreditRtn();
        if(response.getCode()== 200) {
            rtn.setCode(String.valueOf(CommonConstant.SC_OK_200));
            rtn.setSuccess(true);
            rtn.setMsg(response.getMsg());
            JSONObject jsonData=response.getData();
            CreditRtn.CreditRtnData rtnData = jsonData.toJavaObject(CreditRtn.CreditRtnData.class);
            rtnData.setUpdateDate(DateUtils.getDate("yyyy-MM-dd HH:mm:ss"));
            rtn.setData(rtnData);
        }else{
            String msg=response.getMsg();
            if(msg.contains("重新登录")){
                msg="终端："+commonParam.getKpzd().getZdmc()+"对应的电票平台登录失效，"+msg;
                rtn.setCode(String.valueOf(CommonConstant.DPPT_DL_SX));
            }else{
                rtn.setCode(String.valueOf(CommonConstant.SC_INTERNAL_SERVER_ERROR_500));
            }
            rtn.setSuccess(false);
            rtn.setMsg(msg);
        }
        return rtn;
    }

    /**
     * 公用返回转换
     * @param response
     * @return
     */
    public static CommonRtn commonRtnConvert(String response){
        CommonRtn rtn=new CommonRtn();
        if(StringUtils.isNotEmpty(response)){
            ZhiyunApiResponse zhiyunApiResponse=JSONObject.parseObject(response,ZhiyunApiResponse.class);
            Integer code=zhiyunApiResponse.getCode();
            if(code==200){
                rtn.setSuccess(true);
                rtn.setMsg(zhiyunApiResponse.getMsg());
            }else{
                rtn.setSuccess(false);
                rtn.setMsg(zhiyunApiResponse.getMsg());
            }
        }else{
            rtn.setSuccess(false);
            rtn.setMsg("电票平台接口调用超时，请稍后重试");
        }
        return rtn;
    }

    /**
     * 申请全电票红字信息表返回转换
     * @param response
     * @return
     */
    public static SdSqqdphzxxbcc13Rtn sdSqqdphzxxbcc13RtnConvert(String response){
        SdSqqdphzxxbcc13Rtn rtn=new SdSqqdphzxxbcc13Rtn();
        if(StringUtils.isNotEmpty(response)){
            ShudianResponse<SdSqqdphzxxbcc13Rtn> zhiyunApiResponse=JSONObject.parseObject(response,new TypeReference<ShudianResponse<SdSqqdphzxxbcc13Rtn>>(){});
            Integer code=zhiyunApiResponse.getCode();
            if(code==200){
                rtn=zhiyunApiResponse.getData();
                rtn.setSuccess(true);
                rtn.setMsg(zhiyunApiResponse.getMsg());
            }else{
                rtn.setSuccess(false);
                rtn.setMsg(zhiyunApiResponse.getMsg());
            }
        }else{
            rtn.setSuccess(false);
            rtn.setMsg("电票平台接口调用超时，请稍后重试");
        }
        return rtn;
    }

    /**
     * 查询红字信息表返回转换
     * @param response
     * @return
     */
    public static SdCxhzxxbcc112Rtn sdCxhzxxbcc112RtnConvert(String response){
        SdCxhzxxbcc112Rtn rtn=new SdCxhzxxbcc112Rtn();
        if(StringUtils.isNotEmpty(response)){
            ZhiyunApiResponse zhiyunApiResponse=JSONObject.parseObject(response,ZhiyunApiResponse.class);
            Integer code=zhiyunApiResponse.getCode();
            if(code==200){
                rtn=zhiyunApiResponse.getData().toJavaObject(SdCxhzxxbcc112Rtn.class);
                rtn.setSuccess(true);
                rtn.setMsg(zhiyunApiResponse.getMsg());
            }else{
                rtn.setSuccess(false);
                rtn.setMsg(zhiyunApiResponse.getMsg());
            }
        }else{
            rtn.setSuccess(false);
            rtn.setMsg("电票平台接口调用超时，请稍后重试");
        }
        return rtn;
    }

    /**
     * 全电票开负数发票返回转换
     * @param response
     * @return
     */
    public static SdQdpkfsfpcc14Rtn sdQdpkfsfpcc14RtnConvert(String response){
        SdQdpkfsfpcc14Rtn rtn=new SdQdpkfsfpcc14Rtn();
        if(StringUtils.isNotEmpty(response)){
            ZhiyunApiResponse zhiyunApiResponse=JSONObject.parseObject(response,ZhiyunApiResponse.class);
            Integer code=zhiyunApiResponse.getCode();
            if(code==200){
                JSONObject jsonData = zhiyunApiResponse.getData();
                rtn = jsonData.toJavaObject(SdQdpkfsfpcc14Rtn.class);
                String kqrq = jsonData.getString("kprq");
                Date kprq = null;
                try {
                    kprq = DateUtils.parseDate(kqrq, "yyyy-MM-dd HH:mm:ss");
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                rtn.setKprq(kprq);
                rtn.setSuccess(true);
                rtn.setMsg(zhiyunApiResponse.getMsg());
            }else{
                rtn.setSuccess(false);
                rtn.setMsg(zhiyunApiResponse.getMsg());
            }
        }else{
            rtn.setSuccess(false);
            rtn.setMsg("电票平台接口调用超时，请稍后重试");
        }
        return rtn;
    }

    /**
     * 获取数电纸质发票当前发票号、发票代码返回转换
     * @param response
     * @return
     */
    public static CurrentFphmRtn SdHqqdzzfpdmhmcc123RtnConvert(String response){
        CurrentFphmRtn rtn=new CurrentFphmRtn();
        if(StringUtils.isNotEmpty(response)){
            ZhiyunApiResponse zhiyunApiResponse=JSONObject.parseObject(response,ZhiyunApiResponse.class);
            Integer code=zhiyunApiResponse.getCode();
            if(code==200){
                SdHqqdzzfpdmhmcc123Rtn data=zhiyunApiResponse.getData().toJavaObject(SdHqqdzzfpdmhmcc123Rtn.class);
                rtn.setSuccess(true);
                CurrentFphmRtn.CurrentFphmRtnData currentFphmRtnData=new CurrentFphmRtn.CurrentFphmRtnData();
                BeanUtils.copyProperties(data,currentFphmRtnData);
                currentFphmRtnData.setFplxdm(data.getZpFppzDm());
                rtn.setMsg(zhiyunApiResponse.getMsg());
                rtn.setData(currentFphmRtnData);
            }else{
                rtn.setSuccess(false);
                rtn.setMsg(zhiyunApiResponse.getMsg());
            }
        }else{
            rtn.setSuccess(false);
            rtn.setMsg("电票平台接口调用超时，请稍后重试");
        }
        return rtn;
    }
}
